# Copyright (c) 2010 Chris Moyer http://coredumped.org
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,-
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

# File: resources/comment.py
from boto.sdb.db.model import Model
from boto.sdb.db.property import *
from resources.post import Post
class Comment(Model):
	"""A simple Comment object"""
	post = ReferenceProperty(Post, collection_name='comments')
	tag = StringProperty(choices=['', 'spam', 'ham'], default='')

	# This is just a string since we
	# don't require them to log in to post
	posted_by = StringProperty()
	content = BlobProperty()
	created_at = DateTimeProperty(auto_now_add=True)

	# We set this so we only process this message once
	processed = BooleanProperty(default=False)

	# This is the chance this post is spam
	spam_chance = IntegerProperty(default=0)

	def submit(self):
		"""Submit this comment for approval"""
		import boto
		sqs = boto.connect_sqs()
		q = sqs.get_queue("spamfilter")
		return q.write(q.new_message(self.id))

	def put(self):
		"""We override the put (save) method
		to also make sure it's submitted to 
		our SQS queue for approval"""
		Model.put(self)
		# We must submit after we're saved since 
		# that's when we'll have an ID
		if not self.processed:
			self.submit()
